/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import ClasesBase.TextFilter;
import Extras.GestorEstilos;
import Extras.HeaderCellRenderer;
import Extras.Mensajes;
import Extras.TableCellRendererPropio;
import Gestores.GestorCliente;
import Gestores.GestorServidor;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Jose
 */
public class SubastaD extends javax.swing.JDialog {

    private GestorCliente gestorCliente;
    private GestorServidor gestorServidor;

    public SubastaD(JFrame padre, boolean modo, GestorCliente gestorCliente) {
        super(padre);
        initComponents();
        jTF_oferta.setNextFocusableComponent(this.jB_ofertar);
        jB_ofertar.setNextFocusableComponent(this.jB_noOfertar);
        jB_noOfertar.setNextFocusableComponent(this.jTF_oferta);
        setLocationRelativeTo(null);
        this.gestorCliente = gestorCliente;
        this.setModal(modo);
        this.jT_oferta.getTableHeader().setDefaultRenderer(new HeaderCellRenderer());
        this.jT_oferta.setDefaultRenderer(String.class, new TableCellRendererPropio());
        setearPropiedadesTablas();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlTarjeta = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jT_oferta = new javax.swing.JTable();
        jB_ofertar = new javax.swing.JButton();
        jTF_oferta = new javax.swing.JTextField();
        jL_proximoJugador = new javax.swing.JLabel();
        jB_noOfertar = new javax.swing.JButton();
        jL_precioOriginal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Subasta");
        setMinimumSize(new java.awt.Dimension(750, 348));
        setModal(true);
        setPreferredSize(new java.awt.Dimension(800, 352));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jlTarjeta.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jlTarjeta.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Propiedad:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N

        jT_oferta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Jugador", "Oferta"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jT_oferta);
        if (jT_oferta.getColumnModel().getColumnCount() > 0) {
            jT_oferta.getColumnModel().getColumn(0).setResizable(false);
            jT_oferta.getColumnModel().getColumn(1).setResizable(false);
        }

        jB_ofertar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jB_ofertar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Botones/OFERTAR ON.png"))); // NOI18N
        jB_ofertar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jB_ofertarMouseReleased(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jB_ofertarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jB_ofertarMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jB_ofertarMousePressed(evt);
            }
        });
        jB_ofertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_ofertarActionPerformed(evt);
            }
        });
        jB_ofertar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jB_ofertarKeyPressed(evt);
            }
        });

        jTF_oferta.setDocument(new TextFilter(TextFilter.DIGITS));
        jTF_oferta.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jTF_oferta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTF_ofertaKeyPressed(evt);
            }
        });

        jL_proximoJugador.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jL_proximoJugador.setText("Jugador Ofertando:");
        jL_proximoJugador.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 0, 0)));

        jB_noOfertar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jB_noOfertar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Botones/ABANDONARSUBASTA ON.png"))); // NOI18N
        jB_noOfertar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jB_noOfertarMouseReleased(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jB_noOfertarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jB_noOfertarMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jB_noOfertarMousePressed(evt);
            }
        });
        jB_noOfertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_noOfertarActionPerformed(evt);
            }
        });
        jB_noOfertar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jB_noOfertarKeyPressed(evt);
            }
        });

        jL_precioOriginal.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jL_precioOriginal.setText("Precio original:");
        jL_precioOriginal.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 102)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jL_precioOriginal, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                    .addComponent(jlTarjeta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .addComponent(jL_proximoJugador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jB_noOfertar, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTF_oferta, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jB_ofertar, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTF_oferta, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jB_ofertar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jB_noOfertar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jL_precioOriginal, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jL_proximoJugador, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jlTarjeta, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))))
                .addGap(18, 18, 18))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static boolean isNumeric(String cadena) {
        try {
            Integer.parseInt(cadena);
            return true;
        } catch (NumberFormatException nfe) {
            return false;
        }
    }

    public static boolean isEntero(double numero) {
        int entero;
        boolean esEntero = true;
        try {
            entero = (int) numero;
        } catch (Exception e) {
            esEntero = false;
        }
        return esEntero;
    }
    private void jB_ofertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_ofertarActionPerformed
        if (jTF_oferta.getText().isEmpty()) {
            Mensajes.mostrarError("Debe ingresar un valor para poder ofertar");
            return;
        }
        int cantidadDeOfertas = jT_oferta.getRowCount();
        int ultimaOfertaGeneral = -1;
        if (cantidadDeOfertas > 0) {
            ultimaOfertaGeneral = Integer.parseInt(jT_oferta.getValueAt(cantidadDeOfertas - 1, 1).toString());
        }

        if (isNumeric(jTF_oferta.getText())) {
            int ultimaOferta = Integer.parseInt(jTF_oferta.getText());
            if ((ultimaOferta > ultimaOfertaGeneral)) {
                if (ultimaOferta > 0) {
                    if (ultimaOferta <= gestorCliente.getDineroEfectivo()) {
                        gestorCliente.enviarOferta(ultimaOferta);
                        this.setParticipa(false);
                    } else {
                        Mensajes.mostrarError("La oferta no puede ser mayor a su dinero en efectivo. Por favor corrija el valor e intente nuevamente.");
                    }
                } else {
                    Mensajes.mostrarError("La oferta debe ser mayor a 0. Por favor corrija el valor e intente nuevamente.");
                }
            } else {
                Mensajes.mostrarError("La oferta debe ser mayor a la ultima oferta general. Por favor corrija el valor e intente nuevamente.");
            }
        } else {
            Mensajes.mostrarError("La oferta debe ser numerica. Por favor corrija el valor e intente nuevamente.");
        }
    }//GEN-LAST:event_jB_ofertarActionPerformed

    private void jB_noOfertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_noOfertarActionPerformed
        gestorCliente.enviarOferta(-1);
        this.setParticipa(false);
    }//GEN-LAST:event_jB_noOfertarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        gestorCliente.enviarOferta(-1);
        this.setParticipa(false);
    }//GEN-LAST:event_formWindowClosing

    private void jTF_ofertaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_ofertaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.jB_ofertarActionPerformed(null);
        }
    }//GEN-LAST:event_jTF_ofertaKeyPressed

    private void jB_ofertarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jB_ofertarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.jB_ofertarActionPerformed(null);
        }
    }//GEN-LAST:event_jB_ofertarKeyPressed

    private void jB_noOfertarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jB_noOfertarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.jB_noOfertarActionPerformed(null);
        }
    }//GEN-LAST:event_jB_noOfertarKeyPressed

    private void jB_ofertarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_ofertarMouseEntered
        if(jB_ofertar.isEnabled())
        {
        jB_ofertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/OFERTAR SEL.png")));
        }
    }//GEN-LAST:event_jB_ofertarMouseEntered

    private void jB_ofertarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_ofertarMouseExited
        if(jB_ofertar.isEnabled())
        {
        jB_ofertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/OFERTAR ON.png")));
        }
    }//GEN-LAST:event_jB_ofertarMouseExited

    private void jB_ofertarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_ofertarMousePressed
        if(jB_ofertar.isEnabled())
        {
        jB_ofertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/OFERTAR OFF.png")));
        }
    }//GEN-LAST:event_jB_ofertarMousePressed

    private void jB_ofertarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_ofertarMouseReleased
        if(jB_ofertar.isEnabled())
        {
        jB_ofertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/OFERTAR ON.png")));
        }
    }//GEN-LAST:event_jB_ofertarMouseReleased

    private void jB_noOfertarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_noOfertarMouseEntered
        if(jB_noOfertar.isEnabled())
        {
        jB_noOfertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ABANDONARSUBASTA SEL.png")));
        }
    }//GEN-LAST:event_jB_noOfertarMouseEntered

    private void jB_noOfertarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_noOfertarMouseExited
        if(jB_noOfertar.isEnabled())
        {
        jB_noOfertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ABANDONARSUBASTA ON.png")));
        }
    }//GEN-LAST:event_jB_noOfertarMouseExited

    private void jB_noOfertarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_noOfertarMousePressed
        if(jB_noOfertar.isEnabled())
        {
        jB_noOfertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ABANDONARSUBASTA OFF.png")));
        }
    }//GEN-LAST:event_jB_noOfertarMousePressed

    private void jB_noOfertarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jB_noOfertarMouseReleased
        if(jB_noOfertar.isEnabled())
        {
        jB_noOfertar.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Botones/ABANDONARSUBASTA ON.png")));
        }
    }//GEN-LAST:event_jB_noOfertarMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_noOfertar;
    private javax.swing.JButton jB_ofertar;
    private javax.swing.JLabel jL_precioOriginal;
    private javax.swing.JLabel jL_proximoJugador;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTF_oferta;
    private javax.swing.JTable jT_oferta;
    private javax.swing.JLabel jlTarjeta;
    // End of variables declaration//GEN-END:variables

    public void setImagenTerreno(String idTerreno) {
        String arch = idTerreno + "A.jpg";
        jlTarjeta.setIcon(new ImageIcon(getClass().getResource("/Imagenes/Tablero/Nuevas medidas/" + arch)));
    }

    public void setParticipa(boolean b) {
        jTF_oferta.setEnabled(b);
        jB_ofertar.setEnabled(b);
        jB_noOfertar.setEnabled(b);
        if (!b) {
            jTF_oferta.setText("");
        }
    }

    public void setTitulo(String terreno) {
        this.setTitle(terreno);
    }

    public JTable getTabla() {
        return jT_oferta;
    }

    public JLabel getjL_proximoJugador() {
        return jL_proximoJugador;
    }

    public void setPrecio(String precio) {
        jL_precioOriginal.setText(jL_precioOriginal.getText() + " $" + precio);
    }

    public void setjL_proximoJugador(JLabel jL_proximoJugador) {
        this.jL_proximoJugador = jL_proximoJugador;
    }

    public void setFoco() {
        jTF_oferta.grabFocus();
    }
    
    private void setearPropiedadesTablas() {
        HeaderCellRenderer hcr = new HeaderCellRenderer();
        TableCellRendererPropio tcrp = new TableCellRendererPropio();
        this.jT_oferta.setDefaultRenderer(String.class, tcrp);
        this.jT_oferta.getTableHeader().setDefaultRenderer(hcr);
        
        this.jT_oferta.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
}
